package com.kvest.view_pager_test.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;
import com.kvest.maputils.Coordinate;
import com.kvest.maputils.FindPathResult;
import com.kvest.maputils.PathFinder;
import com.kvest.view_pager_test.R;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Kvest
 * Date: 21.03.13
 * Time: 22:36
 * To change this template use File | Settings | File Templates.
 */
public class CustomMapFragment extends Fragment {
    private static final double DEFAULT_LATITUDE = 48.1960067d;
    private static final double DEFAULT_LONGITUDE = 16.341749d;

    private GoogleMap map = null;
    SupportMapFragment smf;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.map_layout, null);
        smf = SupportMapFragment.newInstance();

        getFragmentManager().beginTransaction().replace(R.id.cont, smf).commit();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException interruptedException){};
//                    PathFinder pathFinder = new PathFinder(new Coordinate(46.466666676667,30.733333343333),
//                            new Coordinate(46.966666676667,32.00000001));
                    PathFinder pathFinder = new PathFinder(new Coordinate(50.41989,30.48391),
                            new Coordinate(50.35814,30.94964));
                    final FindPathResult res = pathFinder.findPath();

                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (map == null) {
                                map = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.cont)).getMap();
                            }

                            if (res.isFound() && map != null) {
                                map.clear();

                                PolylineOptions rectOptions = new PolylineOptions();

                                List<Coordinate> points = res.getPoints();
                                for (int i = 0; i < points.size(); ++i) {
                                    LatLng position = new LatLng(points.get(i).getLatitude(), points.get(i).getLongitude());
                                    map.addMarker(new MarkerOptions().position(position).title(Integer.toString(i)));

                                    rectOptions.add(position);
                                }

                                rectOptions.color(Color.RED);
                                rectOptions.width(2);
                                /*Polyline polyline = */map.addPolyline(rectOptions);
                            }
                        }
                    });
                    Log.d("DISTANCE", Integer.toString(res.getDistance()));
                }catch (Exception ex) {
                    Log.d("NET", ex.getMessage());
                }
            }
        }).start();


        return view;
    }
}
